﻿
var itip = function (op) {
    this.op = op || {
        panel: "A7805CB38C324A02BEA76FC8950F83DC",
        message: 'this is message tips!',
        position: { left: '30%', top: 5 },
        isAutoHide: true,
        displayTime: 3000,
        css: 'errorPanel',
        callback: null
    };
    this.displayTimerID = null;
};
itip.prototype.hide = function () {
    var errorpanel = $("#" + this.op.panel);
    if (errorpanel.length > 0) {
        errorpanel.fadeOut(function() {
            if (this.op.callback && $.isFunction(this.op.callback)) {
                this.op.callback();
            }
        });
    }
    
};

itip.prototype.show = function () {
    //op = $.extend({}, op, {
    //    message: message,
    //    displayTime: displayTime
    //});
    var $errorPanel = $("#" + this.op.panel);
    if ($errorPanel.length == 0) {
        $errorPanel = $("<div />").attr("id", this.op.panel).appendTo("body");
    }

    if (this.op.css && !$errorPanel.hasClass(this.op.css)) {
        $errorPanel.addClass(this.op.css);
    }

    if ($errorPanel.css("display") != "none") {
        $errorPanel.find(">dt").append("<dl style='margin: 0;padding: 3px 0;background:none;'>" + this.op.message + "</dl>");
        if (this.displayTimerID) {
            window.clearTimeout(this.displayTimerID);
        }
    }
    else {
        $errorPanel.html("<dt style='background:none;'><dl style='margin: 0;padding: 3px 0;background:none;'>" + this.op.message + "</dl></dt>");
        $errorPanel.css(this.op.position).fadeIn();
    }


    var thisObj = this;
    if (this.op.isAutoHide) {
        this.displayTimerID = setTimeout(function () {
            thisObj.hide();
        }, this.op.displayTime);
    }
};

itip.prototype.remove = function() {
    var errorpanel = $("#" + this.op.panel);
    if (errorpanel.length > 0) {
        errorpanel.remove();
    }
};

/*
 var m = $.messageTip.instance({
            isAutoHide: false,
            message: 'thank you !~',
            position: {
                left:'50%',
                marginLeft:'-40px',
                top:'100px'
            }
        })

        $(function () {

            $("#btnOpen").click(function () {
                m.show();
            });

            $("#btnHide").click(function () {
                m.hide();
            });

            $("#btnRemove").click(function () {
                m.remove();
            });
        });
         <style type="text/css">
        .errorPanel
        {
            position: absolute;
            padding: 0px 20px;
            background: #68af02;
            display: none;
            color: #fff;
            font-size: 12px;
        }
        </style>
*/
(function ($) {

    var obj = new itip();

    $.extend($, {
        messageTip: function (options) {
            var tip = {};
            var displayTimerID;
            var defaults = {
                message: 'this is message tips!',
                position: { left: '30%', top: 5 },
                isAutoHide: true,
                displayTime: 3000,
                css: 'errorPanel'
            };
            var op = $.extend({}, defaults, options);

            tip.hide = function (callback) {
                var errorpanel = $("#errorPanelzhangQ");
                if (errorpanel.length > 0) {
                    errorpanel.fadeOut(function() {
                        if (callback && $.isFunction(callback)) {
                            callback();
                        }
                        errorpanel.remove();
                    });
                }
                
            };

            tip.remove = function () {
                var errorpanel = $("#errorPanelzhangQ");
                if (errorpanel.length > 0) {
                    errorpanel.remove();
                }
            };


            tip.show = function (message, displayTime, callback) {
                op = $.extend({}, op, {
                    message: message,
                    displayTime: displayTime,
                    callback: callback
                });
                var $errorPanel = $("#errorPanelzhangQ");
                if ($errorPanel.length == 0) {
                    $errorPanel = $("<div id='errorPanelzhangQ' />").appendTo("body");
                }

                if (op.css && !$errorPanel.hasClass(op.css)) {
                    $errorPanel.addClass(op.css);
                }

                if ($errorPanel.css("display") != "none") {
                    $errorPanel.find(">dt").append("<dl style='margin: 0;padding: 3px 0;background:none;'>" + op.message + "</dl>");
                    if (displayTimerID) {
                        window.clearTimeout(displayTimerID);
                    }
                }
                else {
                    $errorPanel.html("<dt style='background:none;'><dl style='margin: 0;padding: 3px 0;background:none;'>" + op.message + "</dl></dt>");
                    $errorPanel.css(op.position).fadeIn();
                }



                if (op.isAutoHide) {
                    displayTimerID = setTimeout(function () {
                        tip.hide(op.callback);
                    }, op.displayTime);
                }
            };


            return tip;
        },
        mTip: {
            show: function (options) {
                if (!obj) {
                    obj = new itip();
                }
                if (options) {
                    obj.op = options;
                }
                obj.show();
            },
            hide: function() {
                if (obj) {
                    obj.hide();
                }
            },
            dispose: function() {
                if (obj) {
                    obj.hide();
                    obj.remove();
                    obj = null;
                }
            }
        }
    });

})(jQuery);

